//
//  ShellCoinHeadView.m
//  ShellWallert
//
//  Created by lvzhao on 2020/5/10.
//  Copyright © 2020 VV. All rights reserved.
//

#import "ShellCoinHeadView.h"
#import "ShellCoinViewModel.h"
#import "ShellRechargeViewController.h"
#import "ShellWithdrawViewController.h"


@interface ShellCoinHeadView()

@property (nonatomic,strong) ShellCoinViewModel *viewModel;

@end

@implementation ShellCoinHeadView

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellCoinViewModel *)viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = k_imageName(@"icon_shellcion_joint");
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(K_SCREENWIDTH - 40);
        make.height.mas_equalTo((K_SCREENWIDTH - 40) * 140 /334);
    }];
    
    UIView *assetsView = [[UIView alloc] init];
    assetsView.cornerRadius = 8;
    [assetsView setGradientBackgroundWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x22C6FE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
    [self addSubview:assetsView];
    [assetsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageView.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(K_SCREENWIDTH - 40);
        make.height.mas_equalTo(150);
    }];
    
    UIImageView *logoView = [[UIImageView alloc] initWithImage:k_imageName(@"icon_shellcoin_logo")];
    [self addSubview:logoView];
    [logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(assetsView.mas_centerX);
        make.centerY.mas_equalTo(assetsView.mas_top).mas_offset(-5);
        make.width.mas_equalTo(58);
        make.height.mas_equalTo(58);
    }];
    
    ///!!!:锁定
    UILabel *lockNumLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColor.whiteColor];
    [assetsView addSubview:lockNumLab];
    [lockNumLab lz_centerAlignment];
    [lockNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.width.mas_equalTo((K_SCREENWIDTH - 70)/2.0);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(24);
    }];
    
    ///!!!:自由
    UILabel *freeNumLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColor.whiteColor];
    [assetsView addSubview:freeNumLab];
    [freeNumLab lz_centerAlignment];
    [freeNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(lockNumLab.mas_width);
        make.height.mas_equalTo(lockNumLab);
        make.top.mas_equalTo(lockNumLab);
    }];

    ///!!!:充值
    CGFloat btnW = (K_SCREENWIDTH - 80 - 15)/2.0;
    UIButton *rechargeBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"充值") titleColor:UIColorHex(0x678DA8) fontSize:14];
    rechargeBtn.backgroundColor = UIColorHex(0xFFFFFF);
    rechargeBtn.cornerRadius = 4;
    [assetsView addSubview:rechargeBtn];
    [rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.bottom.mas_equalTo(-20);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(40);
    }];
       
    
    ///!!!:提币
    UIButton *withdrawBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"提币") titleColor:UIColorHex(0xFFFFFF) fontSize:14];
    withdrawBtn.bColor = UIColorHex(0xFFFFFF);
    withdrawBtn.bWidth = 1;
    withdrawBtn.cornerRadius = 4;
    [assetsView addSubview:withdrawBtn];
    [withdrawBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-20);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(40);
    }];

   
    
   //充币
             
    
    
    @weakify(self);
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        
        [rechargeBtn setTitle:LZLocalizedString(@"充值") forState:UIControlStateNormal];
        [withdrawBtn setTitle:LZLocalizedString(@"提币") forState:UIControlStateNormal];

        NSString *freeze = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.freeze].saveDecimal;
        NSString *useable = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.useable].saveDecimal;

        ///锁定
        NSString *lockNum = [NSString stringWithFormat:@"%@",freeze];
        NSString *lockString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"锁定"),lockNum];
        NSMutableAttributedString *lockAttributedString = [NSAttributedString attributedString:lockString rangeTitle:lockNum leftFont:k_textFont(12) rightFont:k_textBoldFont(20) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0xFFFFFF)];
        lockNumLab.attributedText = lockAttributedString;


        ///自由
        NSString *freeNum = [NSString stringWithFormat:@"%@",useable];
        NSString *freeString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"自由"),lockNum];
        NSMutableAttributedString *freeAttributedString = [NSAttributedString attributedString:freeString rangeTitle:freeNum leftFont:k_textFont(12) rightFont:k_textBoldFont(20) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0xFFFFFF)];
        freeNumLab.attributedText = freeAttributedString;
        
        
    }];
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        NSString *freeze = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.freeze].saveDecimal;
        NSString *useable = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.useable].saveDecimal;

        ///锁定
        NSString *lockNum = [NSString stringWithFormat:@"%@",freeze];
        NSString *lockString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"锁定"),lockNum];
        NSMutableAttributedString *lockAttributedString = [NSAttributedString attributedString:lockString rangeTitle:lockNum leftFont:k_textFont(12) rightFont:k_textBoldFont(20) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0xFFFFFF)];
        lockNumLab.attributedText = lockAttributedString;
           
        
        ///自由
       NSString *freeNum = [NSString stringWithFormat:@"%@",useable];
       NSString *freeString = [NSString stringWithFormat:@"%@\n%@",LZLocalizedString(@"自由"),freeNum];
       NSMutableAttributedString *freeAttributedString = [NSAttributedString attributedString:freeString rangeTitle:freeNum leftFont:k_textFont(12) rightFont:k_textBoldFont(20) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0xFFFFFF)];
       freeNumLab.attributedText = freeAttributedString;
        
    }];
    
    
    [[rechargeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        ShellRechargeViewController *rechargeVC = [[ShellRechargeViewController alloc] init];
        rechargeVC.wallertModel = self.viewModel.wallertModel;
        [LZTool.currentViewController.navigationController pushViewController:rechargeVC animated:YES];
        
    }];

    
    [[withdrawBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        ShellWithdrawViewController *coinVC = [[ShellWithdrawViewController alloc] init];
        coinVC.wallertModel = self.viewModel.wallertModel;
        [LZTool.currentViewController.navigationController pushViewController:coinVC animated:YES];
    }];
    
}

@end
